﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Xml;
using System.Windows.Controls;

namespace PhilosophyQuiz
{
	/// <summary>
	/// Interaction logic for Registration.xaml
	/// </summary>
	public partial class Registration : Window
	{
		private Team teamC = new Team();

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			teamC.ClearXml();
		}

		public Registration()
		{
			InitializeComponent();
			teamC.ClearXml();
			dbRegistered.ItemsSource = teamC.List;
		}

		private void buttonAdd_Click(object sender, RoutedEventArgs e)
		{
			if (textBoxTeamName.Text == String.Empty) MessageBox.Show("Трябва да въведете име!");
			else
			{
				XmlDocument team = new XmlDocument();
				team.Load(teamC.path);

				XmlNode node = team.CreateNode(XmlNodeType.Element, "team", team.NamespaceURI);
				team.DocumentElement.AppendChild(node);

				XmlAttribute attr = team.CreateAttribute("Name");
				attr.InnerText = textBoxTeamName.Text;
				//XmlAttribute attrC = team.CreateAttribute("Colour");
				//    string color = ChooseRandomColour();
				//    attrC.InnerText = color;
				XmlAttribute attrClass = team.CreateAttribute("Class");
				attrClass.InnerText = comboClass.SelectionBoxItem.ToString();
				XmlAttribute attrPoints = team.CreateAttribute("Points");
				attrPoints.InnerText = "0";

				node.Attributes.Append(attr);
				node.Attributes.Append(attrClass);
				//node.Attributes.Append(attrC);
				node.Attributes.Append(attrPoints);

				team.Save(teamC.path);
				textBoxTeamName.Text = String.Empty;

				dbRegistered.ItemsSource = teamC.List;
			}
		}

		private string ChooseRandomColour()
		{
			var rand = new Random();
			byte red = Byte.Parse(rand.Next(255).ToString());
			byte green = Byte.Parse(rand.Next(255).ToString());
			byte blue = Byte.Parse(rand.Next(255).ToString());
			Color color = new Color();
			color.R = red;
			color.B = blue;
			color.G = green;
			return color.ToString();
		}

		private void buttonClear_Click(object sender, RoutedEventArgs e)
		{
			teamC.ClearXml();
			dbRegistered.ItemsSource = teamC.List;
		}

		private void buttonStartGame_Click(object sender, RoutedEventArgs e)
		{
			int cl = int.Parse((comboClass.SelectedItem as ComboBoxItem).Tag.ToString());
			var mainWindow = new MainWindow();
			mainWindow.question.ClassNumber = cl;
			mainWindow.Show();
			this.Close();
		}
	}
}
